package com.lanlian.cayman.fragment;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.NumberManager;
import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.commonlib.data.ContractBean;
import com.lanlian.commonlib.data.GoodsBean;
import com.lanlian.commonlib.data.MarketBean;
import com.lanlian.commonlib.data.RespMarket;

/**
 * <pre>
 *     author :
 *
 *     date   : 2018/8/16 10:17
 *     desc   :  k底部切换item 商品信息
 *     version: 1.0
 * </pre>
 */
public class GoodsFragment extends LinearLayout {

    private TextView mTvName;
    private TextView mTvNo;
    private TextView mTvMarket;
    private TextView mTvMoneyNight;
    private TextView mTvMoney;
    private TextView mTvCurrency;
    private TextView mTvLastDay;
    private TextView mTvEnumNight;
    private GoodsBean mGb;
    private ContractBean mContractBean;
    private RespMarket mRespMarket;
    private MarketBean mMarketBean;
    int count = 2;
    private Activity act;

    public GoodsFragment(Context context) {
        super(context);
        act = (Activity) context;
        View view = act.getLayoutInflater().inflate(R.layout.fragment_goods, null);
        initView(view);
        addView(view);
    }

    protected void initView(View view) {
        mTvName = view.findViewById(R.id.tv_name);
        mTvNo = view.findViewById(R.id.tv_no);
        mTvMarket = view.findViewById(R.id.tv_market);
        mTvMoneyNight = view.findViewById(R.id.tv_money_night);
        mTvMoney = view.findViewById(R.id.tv_money);
        mTvCurrency = view.findViewById(R.id.tv_currency);
        mTvLastDay = view.findViewById(R.id.tv_last_day);
        mTvEnumNight = view.findViewById(R.id.tv_enum_night);
    }

    private void loadData() {
        if (mContractBean != null) {
            if (mTvName == null) {
                View view = act.getLayoutInflater().inflate(R.layout.fragment_goods, null);
                initView(view);
            }
            mTvName.setText(mContractBean.contractName);
            mTvNo.setText(mContractBean.contractNo);
            mTvMarket.setText(mMarketBean.marketName);
            mTvCurrency.setText(mGb.currency);
            mTvLastDay.setText(mContractBean.lastTradeDate);
            DBManager dbManager = DBManager.getInstance();
            if (dbManager.isLogined() && dbManager.getUser() != null
                    && dbManager.getUser().getAccountType() == 1) {
                mTvEnumNight.setText("维持保证金");
                mTvMoneyNight.setText(NumberManager.getString4or5("1", mGb.keepDeposit, 0));
                mTvMoney.setText(NumberManager.getString4or5("1", mGb.standardDeposit + "", 0));
            } else {
                mTvEnumNight.setText("隔夜保证金");
                mTvMoneyNight.setText(NumberManager.getString4or5("1", mGb.overNightDeposit, 0));
                mTvMoney.setText(NumberManager.getString4or5("1", mGb.deposit + "", 0));
            }
        }
    }

    public void setData(String id, Activity act) {
        this.act = act;
        if (MarketFragment.getRespMarket().getGoods(id) == null) {
            count = 2;
        } else {
            String scale = MarketFragment.getRespMarket().getGoods(id).minChange;
            count = NumberManager.getNumberDecimalDigits(scale);
        }
        mRespMarket = MarketFragment.getRespMarket();
        if (id != null&&!"".equals(id)) {
            mGb = mRespMarket.getGoods(id);
            mContractBean = mRespMarket.getContract(id);
            mMarketBean = mRespMarket.getMarket(id);
            loadData();
        }

    }

}
